<?php
/**
 * @author Mazvv
 * @package Emptopercantage
 */
class Emptopercantage extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'emptopercantage';
		$this->lang->load('emptopercantage');
		$this->load->model('emptopercantage_model');
		$this->SetNewToolSize('600x300');
		$this->SetEditToolSize('600x300');
		$this->SetDetailsToolSize('600x300');
		$this->SetFindToolSize('600x300');
		
		$this->a_validation_rules['date_doc'] = "required";
		
		$this->a_validation_rules['_employeers_rid'] = "required";         
		$this->a_validation_rules['percantage_value'] = "required";
		$this->a_validation_rules['bdate'] = "required";    
			
		$this->a_validation_fields['date_doc'] = $this->lang->line('DATE_DOC');
		$this->a_validation_fields['bdate'] = $this->lang->line('BDATE');
		$this->a_validation_fields['_employeers_rid'] = $this->lang->line('EMPLOYEER'); 
		$this->a_validation_fields['percantage_value'] = $this->lang->line('PERCANTAGE');  

		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 

		$content['date_doc'] = $this->lang->line('DATE_DOC');
		$content['employeer_name'] = $this->lang->line('EMPLOYEER');
		$content['percantage_value'] = $this->lang->line('PERCANTAGE'); 
		$content['filial_name'] = $this->lang->line('FILIAL'); 
		$content['bdate'] = $this->lang->line('BDATE'); 		
		
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->emptopercantage_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('emptopercantage/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');
			# { date_doc			
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$content['date_doc_field'] =  form_input('date_doc', $this->validation->date_doc?$this->validation->date_doc:date('d.m.Y'));
			# } date_doc
			
			# { _employeers_rid
			$employeers = array(' '=>'');
			foreach($this->emptopercantage_model->GetEmployeersList() as $employeer){
				$employeers[$employeer->rid] = $employeer->employeer_name;
			}
			$content['_employeers_rid'] = $this->RenderLabel('_employeers_rid');
			$content['_employeers_rid_field'] =  form_dropdown('_employeers_rid', $employeers, $this->validation->_employeers_rid?$this->validation->_employeers_rid:'');
			# } _employeers_rid
			
			# { percantage_value			
			$content['percantage_value'] = $this->RenderLabel('percantage_value'); 
			$content['percantage_value_field'] =  form_input('percantage_value', $this->validation->percantage_value?$this->validation->percantage_value:'');
			# } percantage_value

			# { bdate
			$content['bdate'] = $this->RenderLabel('bdate');
			$data = array('name'=>'bdate', 'id'=>'bdate', 'value'=>$this->validation->bdate?$this->validation->bdate:'', 'size'=>'12', 'readonly'=>'readonly'); 
			$content['bdate_field'] =  form_input($data).$this->RenderCalendar('bdate');
			# } bdate
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('emptopercantage/create', $content, True);
		}
		if($insRid = $this->emptopercantage_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->emptopercantage_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT');
			# { date_doc			
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$content['date_doc_field'] =  form_input('date_doc', $this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc);
			# } date_doc
			
			# { _employeers_rid
			$employeers = array(' '=>'');
			foreach($this->emptopercantage_model->GetEmployeersList() as $employeer){
				$employeers[$employeer->rid] = $employeer->employeer_name;
			}
			$content['_employeers_rid'] = $this->RenderLabel('_employeers_rid');
			$content['_employeers_rid_field'] =  form_dropdown('_employeers_rid', $employeers, $this->validation->_employeers_rid?$this->validation->_employeers_rid:$content['DS']->_employeers_rid);
			# } _employeers_rid
			
			# { percantage_value			
			$content['percantage_value'] = $this->RenderLabel('percantage_value');
			$content['percantage_value_field'] =  form_input('percantage_value', $this->validation->percantage_value?$this->validation->percantage_value:$content['DS']->percantage_value);
			# } percantage_value

			# { bdate
			$content['bdate'] = $this->RenderLabel('bdate');
			$data = array('name'=>'bdate', 'id'=>'bdate', 'value'=>$this->validation->bdate?$this->validation->bdate:$content['DS']->bdate, 'size'=>'12', 'readonly'=>'readonly'); 
			$content['bdate_field'] =  form_input($data).$this->RenderCalendar('bdate');
			# } bdate
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('emptopercantage/edit', $content, True);
		}
		if($this->emptopercantage_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->emptopercantage_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS');
			# { date_doc			
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$content['date_doc_field'] =  form_input('date_doc', $this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc);
			# } date_doc
			
			# { _employeers_rid
			$employeers = array(' '=>'');
			foreach($this->emptopercantage_model->GetEmployeersList() as $employeer){
				$employeers[$employeer->rid] = $employeer->employeer_name;
			}
			$content['_employeers_rid'] = $this->lang->line('EMPLOYEER');
			$content['_employeers_rid_field'] =  form_dropdown('_employeers_rid', $employeers, $this->validation->_employeers_rid?$this->validation->_employeers_rid:$content['DS']->_employeers_rid);
			# } _employeers_rid
			
			# { percantage_value			
			$content['percantage_value'] = $this->lang->line('PERCANTAGE'); 
			$content['percantage_value_field'] =  form_input('percantage_value', $this->validation->percantage_value?$this->validation->percantage_value:$content['DS']->percantage_value);
			# } percantage_value

			# { bdate
			$content['bdate'] = $this->lang->line('BDATE');
			$data = array('name'=>'bdate', 'id'=>'bdate', 'value'=>$this->validation->bdate?$this->validation->bdate:'', 'size'=>'12', 'readonly'=>'readonly'); 
			$content['bdate_field'] =  form_input($data).$this->RenderCalendar('bdate');
			# } bdate
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('emptopercantage/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->emptopercantage_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_FIND')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND'); 

			# { _employeers_rid
			$employeers = array(' '=>'');
			foreach($this->emptopercantage_model->GetEmployeersList() as $employeer){
				$employeers[$employeer->rid] = $employeer->employeer_name;
			}
			$content['_employeers_rid'] = $this->lang->line('EMPLOYEER');
			$content['_employeers_rid_field'] =  form_dropdown('_employeers_rid', $employeers);
			# } _employeers_rid
						
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('  '=>'', '0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
			
			return $this->load->view('emptopercantage/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['_employeers_rid']) && trim($_POST['_employeers_rid'])) $searchRule['_emp_to_percantage_rows._employeers_rid'] = trim($_POST['_employeers_rid']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_users.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_users.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('emptopercantage', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
}

?>